package com.yaj.myfactorysupport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaj.myfactorysupport.entity.TmMounter;
import com.yaj.myfactorysupport.mapper.TmMounterMapper;
import com.yaj.myfactorysupport.service.TmMounterService;
import com.yaj.myfactorysupport.util.ErrorCodeEnum;
import com.yaj.myfactorysupport.util.RespBean;
import com.yaj.myfactorysupport.vo.TmMounterVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wei
 * @since 2021-05-31
 */
@Service
public class TmMounterServiceImpl extends ServiceImpl<TmMounterMapper, TmMounter> implements TmMounterService {
    @Resource
    private TmMounterMapper tmMounterMapper;

    @Override
    public RespBean GetMounterList() {
        List<TmMounter> tmMounters = tmMounterMapper.selectList(null);
        List<TmMounterVo> tmMounterVos = new ArrayList<TmMounterVo>();

        if(ObjectUtil.isNull(tmMounters)){
            return RespBean.error(ErrorCodeEnum.无法取得实装机一览.getCode(),ErrorCodeEnum.无法取得实装机一览.getMsg());
        }

        for(TmMounter tmMounter:tmMounters){
            TmMounterVo tmMounterVo = new TmMounterVo();
            BeanUtil.copyProperties(tmMounter,tmMounterVo);
            tmMounterVos.add(tmMounterVo);
        }

        return RespBean.success(tmMounterVos);
    }

    @Override
    public RespBean GetMounterByLineId(String lineId) {
        TmMounter tmMounter = tmMounterMapper.selectOne(new QueryWrapper<TmMounter>().lambda().eq(TmMounter::getLineId, lineId));
        TmMounterVo tmMounterVo = new TmMounterVo();

        if(ObjectUtil.isNull(tmMounter)){
            return RespBean.error(ErrorCodeEnum.无法取得该产线的实装机.getCode(),ErrorCodeEnum.无法取得该产线的实装机.getMsg());
        }

        BeanUtil.copyProperties(tmMounter,tmMounterVo);

        return RespBean.success(tmMounterVo);
    }
}
